import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Request,
  Req,
  Res,
  Header,
  Headers,
  ParseIntPipe,
  ParseUUIDPipe,
  UseGuards,
  SetMetadata,
  Query,
} from '@nestjs/common';
import { UsersService } from './users.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { PPipe } from './p.pipe';
import * as uuid from 'uuid';
import { RoleGuard } from './role.guard';
import { ReqUrl, Role } from './role.decorator';

console.log(uuid.v4());

@Controller('users')
// @UseGuards(RoleGuard)
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Get('code')
  getCode(@Req() req, @Res() res) {
    return this.usersService.createCode(req, res);
  }

  @Post('create')
  createUser(@Req() req, @Body() body, @Headers() headers) {
    return this.usersService.createUser(req, body, headers);
  }

  @Post()
  // create(@Body(PPipe) createUserDto: CreateUserDto) {
  create(@Body() createUserDto: CreateUserDto) {
    return this.usersService.create(createUserDto);
  }

  @Get(':id')
  // findOne(@Param('id', ParseIntPipe) id: number) {
  findOne(@Param('id', ParseIntPipe) id: string) {
    return this.usersService.findOne(+id);
  }

  // @Get()
  // @UseGuards(RoleGuard)
  // @SetMetadata('role', ['admin'])
  // findAll(@Request() req) {
  //   console.log(req.query);
  //   return this.usersService.findAll();
  // }

  // @Get()
  // @UseGuards(RoleGuard)
  // @Role(['admin'])
  // findAll(@Request() req: Request) {
  //   console.log(req.url);
  //   return this.usersService.findAll();
  // }

  @Get()
  @UseGuards(RoleGuard)
  @Role(['admin'])
  findAll(@ReqUrl() url) {
    console.log(url);
    return this.usersService.findAll();
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.usersService.update(+id, updateUserDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.usersService.remove(+id);
  }
}
